#!/bin/bash
#
#MongoDB的安装脚本,该脚本用来安装3.6版本的rpm包。
#
#
#
#权限检查方法.
function permissionCheck(){
  if [ `id -u` -gt 0 ];
    then
      echo 'please use  the root user to execute this script';
      exit 2;
  fi
}
#检查当前是否安装了软件包
function installed(){
 rpm -qa |grep $1 || yum list installed |grep $1;

}
#下载网址
downloadUrl=http://repo.mongodb.org/yum/redhat/7Server/mongodb-org/3.6/x86_64/RPMS/
#前缀
prefix=mongodb-org
#后缀
version=-3.6.0-1.el7.x86_64
suffix=.rpm
#定义文件下载目录
defaultDownloadDir=/tmp/
#检查当前是否以ROOT身份运行.
permissionCheck;
#需要安装的软件集合
#declare -a softs=("" -mongos -server -shell -tools);
softs[0]="-mongos"
softs[1]="-server"
softs[2]="-shell"
softs[3]="-tools"
softs[4]=""
#校验当前是否已经安装了该软件.
declare -a needInstallSoft;
echo "准备安装的软件:${softs[*]}"
nsize=0;
for ((i=0;i<${#softs[@]};i++))
  do
    softFullName=$prefix${softs[i]}$version;
    echo "校验文件是否已经安装:$softFullName..."
    if [ `rpm -qa |grep $softFullName || yum list installed |grep $softFullName` ]
      then 
        echo "Soft $softFullName installed";
    else
        echo "$softFullName未安装!"
        needInstallSoft[$nsize]=${softs[i]};
        let nsize++;
    fi
done
unset softs;
if [ 0 -eq ${#needInstallSoft[@]} ]
  then 
    echo "当前所有软件均已安装，无需再次安装！";
    exit 0;
fi
echo "需要安装的软件:${needInstallSoft[*]}"
#准备安装环境
ls /tmp >>/dev/null || mkdir /tmp
cd /tmp
#下载文件
for ((i=0;i<${#needInstallSoft[@]};i++))
  do
#判断文件是否已经下载,如果已经下载则不需要下载
    softFullName=$prefix${needInstallSoft[i]}$version$suffix;
    ls $softFullName
    if [ 0 -eq $?  ] 
      then 
        echo "$softFullName 已经下载,无需再次下载!"
        continue;
    fi
    wget http://repo.mongodb.org/yum/redhat/7Server/mongodb-org/3.6/x86_64/RPMS/$softFullName;
    if [ 0 -eq $? ] 
     then echo "$softFullName is download!";
    else echo "$softFullName download is fail!"
    fi
done
#安装文件
for ((i=0;i<${#needInstallSoft[@]};i++))
  do
    softFullName=$prefix${needInstallSoft[i]}$version$suffix;
    rpm -ivh $softFullName
#移除下载的文件
    rm -rf $softFullName
done
wget https://gitee.com/topanda/note/raw/master/linux/mongod.cnf
mv mongod.cnf /etc/mongod.conf
#移除下载的文件
rm -rf /mongod.conf
mkdir -p /data/mongodb/
#修改用户访问权限
chown -R mongod /data/mongodb
